package org.whuims.expertreader.excelreader;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Qikai Cheng
 * @date 五月 16 2018
 */
public class ExtractZhongtu {
    private Pattern blockPattern = Pattern.compile("<ul id=\"list\".+?>(.+?)</ul>");
    private Pattern unitPattern = Pattern.compile("<li><span class=\"code\">(.+?)</span><a href=\"http://www.ztflh.com/\\?c=(\\d+)\">(.+?)</a></li>");
    private StringBuilder sb = new StringBuilder();

    public static void main(String[] args) {
        ExtractZhongtu extractZhongtu = new ExtractZhongtu();
        extractZhongtu.work();
    }

    private void work() {
        File dir = new File("E:\\中图分类发\\www.ztflh.com");
        for (File file : dir.listFiles()) {
            String fileName = file.getName();
            if (fileName.contains("c=")) {
                try {
                    processFile(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            FileUtils.write(new File("d:\\topicinsert.sql"), sb.toString().trim());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void processFile(File file) throws IOException {
        String fileName = file.getName();
        Integer parentId = Integer.parseInt(fileName.split("c=")[1]) + 500000;

        String content = FileUtils.readFileToString(file);
        Matcher blockMatcher = blockPattern.matcher(content);
        String block = "";
        while (blockMatcher.find()) {
            block = blockMatcher.group(1);
        }
        block = block.trim();
        if (block.equalsIgnoreCase("")) {
            return;
        }
        Matcher unitMatcher = unitPattern.matcher(block);
        while (unitMatcher.find()) {
            String code = unitMatcher.group(1);
            String idStr = unitMatcher.group(2);
            Integer id = Integer.parseInt(idStr) + 500000;
            String name = unitMatcher.group(3);
            sb.append("insert into topic values(" + id + ",'" + code + "','" + name + "'," + parentId + ");\n");
        }
    }
}
